package com.monkeyk.sos.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.monkeyk.sos.domain.model.ResourceDetail;
import com.monkeyk.sos.service.ResourceService;
import com.monkeyk.sos.util.Pager;
import com.monkeyk.sos.util.PaginationHelper;

@Service("resourceService")
public class ResourceImpService implements ResourceService {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;


     /**
      * 通过id来删除 resource资源
      * @param id
      */
    public void deleteResouceDetailByID(Long id) {
	String sql = "delete from resource where id="+id;
	jdbcTemplate.execute(sql);
	
    }
    
    /** 
     * 得到所有的资源信息
     * @author minwei
     * @return List<ResourceDetail>
     */
    public List<ResourceDetail> getAllResouceList() {
	String  sql = "select * from resource where 1=1";
	List<ResourceDetail> resourceDetailList=this.jdbcTemplate.query(sql, 
		new RowMapper<ResourceDetail>() {
                	  @Override
                	  public ResourceDetail mapRow(ResultSet rs, int rowNum)throws SQLException {
                	      ResourceDetail   resourceDetail = new ResourceDetail();
                	      resourceDetail.setId(rs.getLong("id"));
                	      resourceDetail.setIds(rs.getString("ids"));
                	      resourceDetail.setName(rs.getString("name"));
                	   return resourceDetail;
                	    }
	    
	});
	return resourceDetailList;
    }
    
    /** 
     * 得到选择的资源
     * @author minwei
     * @return List<ResourceDetail>
     */
    public List<ResourceDetail> getSelectedResourceDetail(ResourceDetail rd,Pager pagingInfo) {
	String  sql = "select * from resource  limit "+(pagingInfo.getPage()-1)*pagingInfo.getPageSize()+","+pagingInfo.getPageSize();
	List<ResourceDetail> resourceDetailList=this.jdbcTemplate.query(sql, 
		new RowMapper<ResourceDetail>() {
                	  @Override
                	  public ResourceDetail mapRow(ResultSet rs, int rowNum)throws SQLException {
                	      ResourceDetail   resourceDetail = new ResourceDetail();
                	      resourceDetail.setId(rs.getLong("id"));
                	      resourceDetail.setIds(rs.getString("ids"));
                	      resourceDetail.setName(rs.getString("name"));
                	   return resourceDetail;
                	    }
	    
	});
	return resourceDetailList;
    }
    
    @Override
    public List<String> findInterfaceURLByID(Long id) {
	// TODO Auto-generated method stub
	return null;
    }

    /**
     * 得到所有合作者数据总数
     * 
     * @return int
     */
    public int getAllCooperDetailsTotals() {
	String sqlCountRows = "select count(*) from resource";
	return PaginationHelper.findTotalCount(this.jdbcTemplate, sqlCountRows,
		null);
    }

    public void addResouceDetail(ResourceDetail rd) {

    }

    @Override
    public void updateResourceDetail(ResourceDetail rd) {
	// TODO Auto-generated method stub
	
    }
	
    
}